package com.dfhc.util;

import java.io.File;
import java.io.FilenameFilter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 通用的文件名过滤器
 * 
 * 作者:龙色波 日期:2013-12-3
 */
public class CommentFileFilter implements FilenameFilter {
	private String filterName;
	private Pattern p =null;
	public CommentFileFilter(String filterName) {
		this.filterName = filterName;
		p = Pattern.compile(filterName);
	}


	@Override
	public boolean accept(File dir, String name) {
		if (name != null && filterName != null) {
			//如果是目录则接受，否则匹配
			File testFile = new File(dir.getAbsolutePath()+File.separator+name);
			if(testFile.isDirectory()){
				return true;
			}else{
				Matcher matcher = p.matcher(name);
				if (matcher.matches()) {
					return true;
					// 否则，则文件名要完全相同
				} else {
					return name.equals(filterName);
				}
			}
		} else {
			return true;
		}
	}	


}